package com.zhijian.medical.config.datasource;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;


/**
 * 多数据源配置
 *
 * @author HCG
 * @version 1.0
 * @date 2021/8/31
 */
@Configuration
public class DataSourceConfig {

    /**
     * local
     *
     * @return
     */
    @Primary
    @Bean(name = "localDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.local")
    public DataSourceProperties dbApplicationDataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * local
     *
     * @param dataSourceProperties
     * @return
     */
    @Primary
    @Bean(name = "localDataSource")
    public DataSource localDataSource(@Qualifier("localDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    /**
     * remote
     *
     * @return
     */
    @Primary
    @Bean(name = "remoteDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.remote")
    public DataSourceProperties DataSourceProperties() {
        return new DataSourceProperties();
    }

    /**
     * db_oss
     *
     * @param dataSourceProperties
     * @return
     */
    @Primary
    @Bean(name = "remoteDataSource")
    public DataSource dbOssDataSource(@Qualifier("remoteDataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

}
